package com.miracle.yuanshen;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;

import java.util.List;

public class MyAdapter extends ArrayAdapter<AddressInfoBean> {

    private Context context;
    private int resourceId;



    public MyAdapter(@NonNull Context context, int resource, @NonNull List<AddressInfoBean> objects) {
        super(context, resource, objects);
        Log.e("MyAdapter -> ", "初始化viewList");
        this.context = context;
        resourceId = resource;
    }

    /**
     * view代表其中一个控件对象
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final View view_ret;

        //view为空则加载布局,不为空则重用
        if (view == null) {
            view_ret = LayoutInflater.from(getContext()).inflate(resourceId,null);
        } else {
            view_ret = view;
        }

        AddressInfoBean item = getItem(i);
        //将ViewHolder存储在View中
        Address_holder address_holder = new Address_holder();
        address_holder.address_name = view_ret.findViewById(R.id.address_name);
        address_holder.address_address = view_ret.findViewById(R.id.address_address);
        address_holder.address_valid=view_ret.findViewById(R.id.address_valid);
        view_ret.setTag(address_holder);
        //给视图赋值
        address_holder.address_name.setText( item.name);
        address_holder.address_address.setText( item.address);
        address_holder.address_valid.setChecked(item.valid);
        return view_ret;
    }

    class Address_holder{
        TextView address_name;
        TextView address_address;
        Switch address_valid;
    }

}
